<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package        CodeIgniter
 * @author        ExpressionEngine Dev Team
 * @copyright    Copyright (c) 2006, EllisLab, Inc.
 * @license        http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since        Version 1.0
 * @filesource
 */

/**
 * CodeIgniter Common Helpers
 *
 * @package        CodeIgniter
 * @subpackage    Helpers
 * @category    Helpers
 * @author        Ario Sutomo
 * @link        
 */

/**
 * Cek apakah suatu data terkait dengan data lain atau tidak
 *
 * Cek apakah suatu data terkait dengan data lain atau tidak
 * 
 * @access    public
 * @return    string
 */    
if (! function_exists('is_bisa_dihapus'))
{
    function is_bisa_dihapus($tabel, $where) 
    {
        $CI =& get_instance();
        
        $jum = 0;
        foreach($tabel as $t) {
            $CI->db->where( $where );
            $CI->db->from( $t );
            $jum += $CI->db->count_all_results();
        }
        
		$ret = ($jum == 0 ? 1 : 0);
		return $ret;
    }
}




/**
 * Combo Bulan
 *
 * Menampilkan list Bulan
 *
 * @access    public
 * @param     string    name
 * @param     string    selected
 * @return    void
 */    
if (! function_exists('combo_bulan'))
{
    function combo_bulan($name, $selected, $class_style='', $extra='') 
    {
		$arr_bulan = array(1=>'Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','Nopember','Desember');
        $CI =& get_instance();
        
        $retval = "<select ".($class_style=="" ? "" : "class=$class_style")." name=$name $extra>";

        for ($i=1; $i<=12; $i++) {
            $retval .= "<option value=\"".$i."\"";
            if ($i==$selected) $retval .= " selected ";
            $retval .= " >".$arr_bulan[$i]."</option>";
        }
        $retval .= "</select>";
		
		return $retval;
    }
}


/**
 * DMY 2 YMD
 *
 * Mengkonversi format tanggal DMY menjadi YMD
 *
 * @access    public
 * @param    string    tgl
 * @param    string    separator
 * @return   string
 */
if (! function_exists('DMY2YMD'))
{
    function DMY2YMD($tgl, $separator)
    {
        $arr = explode($separator, $tgl);
        return sprintf('%s%s%s%s%s', $arr[2], $separator, $arr[1], $separator, $arr[0]);
    }
}

/**
 * MDY 2 YMD
 *
 * Mengkonversi format tanggal datepicker
 *
 * @access    public
 * @param    string    tgl
 * @param    string    separator
 * @return   string
 */
if (! function_exists('MDY2YMD'))
{
    function MDY2YMD($tgl, $separator)
    {
        $arr = explode($separator, $tgl);
        return sprintf('%s%s%s%s%s', $arr[2], $separator, $arr[0], $separator, $arr[1]);
    }
}

/**
 * YMD 2 DMY
 *
 * Mengkonversi format tanggal YMD menjadi DMY
 *
 * @access    public
 * @param    string    tgl
 * @param    string    separator
 * @return   string
 */
if (! function_exists('YMD2DMY'))
{
    function YMD2DMY($tgl, $separator)
    {
        $arr = explode($separator, $tgl);
        if (count($arr)==3) {
            return sprintf('%s%s%s%s%s', $arr[2], $separator, $arr[1], $separator, $arr[0]);
        } else {
            return $tgl;
        }
    }
}


/*
That it is an implementation of the function money_format for the
platforms that do not it bear.

The function accepts to same string of format accepts for the
original function of the PHP.

(Sorry. my writing in English is very bad)

The function is tested using PHP 5.1.4 in Windows XP
and Apache WebServer.
*/
if (! function_exists('money_format')) {
    function money_format($format, $number)  {
        $regex  = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
                  '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
        if (setlocale(LC_MONETARY, 0) == 'C') {
            setlocale(LC_MONETARY, '');
        }
        $locale = localeconv();
        preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
        foreach ($matches as $fmatch) {
            $value = floatval($number);
            $flags = array(
                'fillchar'  => preg_match('/\=(.)/', $fmatch[1], $match) ?
                               $match[1] : ' ',
                'nogroup'   => preg_match('/\^/', $fmatch[1]) > 0,
                'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
                               $match[0] : '+',
                'nosimbol'  => preg_match('/\!/', $fmatch[1]) > 0,
                'isleft'    => preg_match('/\-/', $fmatch[1]) > 0
            );
            $width      = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
            $left       = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
            $right      = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
            $conversion = $fmatch[5];

            $positive = true;
            if ($value < 0) {
                $positive = false;
                $value  *= -1;
            }
            $letter = $positive ? 'p' : 'n';

            $prefix = $suffix = $cprefix = $csuffix = $signal = '';

            $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
            switch (true) {
                case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
                    $prefix = $signal;
                    break;
                case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
                    $suffix = $signal;
                    break;
                case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
                    $cprefix = $signal;
                    break;
                case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
                    $csuffix = $signal;
                    break;
                case $flags['usesignal'] == '(':
                case $locale["{$letter}_sign_posn"] == 0:
                    $prefix = '(';
                    $suffix = ')';
                    break;
            }
            if (!$flags['nosimbol']) {
                $currency = $cprefix .
                            ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
                            $csuffix;
            } else {
                $currency = '';
            }
            $space  = $locale["{$letter}_sep_by_space"] ? ' ' : '';

            $value = number_format($value, $right, $locale['mon_decimal_point'],
                     $flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
            $value = @explode($locale['mon_decimal_point'], $value);

            $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
            if ($left > 0 && $left > $n) {
                $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
            }
            $value = implode($locale['mon_decimal_point'], $value);
            if ($locale["{$letter}_cs_precedes"]) {
                $value = $prefix . $currency . $space . $value . $suffix;
            } else {
                $value = $prefix . $value . $space . $currency . $suffix;
            }
            if ($width > 0) {
                $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
                         STR_PAD_RIGHT : STR_PAD_LEFT);
            }

            $format = str_replace($fmatch[0], $value, $format);
        }
        return $format;
    }
}

    
	/**
	 * trimTime
	 *
	 * Mengambil bagian tanggal saja dari tipe data DATETIME (bagian waktu dihapus)
	 *
	 * @access    public
	 * @param     string    tgl (yyyy-mm-dd hhhh:mm:dd)
	 * @return    integer
	 */    
	if (!function_exists('trimTime')) {
	    function trimTime($tgl) {
	        $temp = explode(' ', $tgl);
	        return $temp[0];
	    }
	}

    /**
	 * getBulan
	 *
	 * Menghasilkan string nama bulan dari nomor bulan 
	 *
	 * @access    public
	 * @param     int       nomor bulan
	 * @return    string
	 */    
	if (!function_exists('getBulan')) {
	    function getBulan($b) {
	    	$arr = array(1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus',
	    	                  'September', 'Oktober', 'Nopember', 'Desember');
	        return $arr[$b*1];
	    }
	}

            
/**
 * YMD2Indo
 *
 * Mengkonversi format tanggal YMD menjadi format D MMM YYYY versi Indonesia
 *
 * @access    public
 * @param    string    tgl
 * @param    string    separator
 * @return   string
 */    
if (! function_exists('YMD2Indo')) {
    function YMD2Indo($tgl, $separator) {
        $bul = array(0 => '00', 1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');
    	$arr = explode($separator, $tgl);
        return sprintf('%d %s %s', $arr[2]*1, $bul[ $arr[1]*1 ], $arr[0]);
    }
}

if (! function_exists('HariIni')) {
    function HariIni() {
        $hari = array(1 => 'Senin', 'Selasa', 'Rabu', 'Kamis', "Jum'at", 'Sabtu', 'Minggu');
        return $hari[ date("N") ];
    }
}


function combo_tahun_umum($name, $selected = '', $tahun_awal = '', $tahun_akhir = '') {
	if ($tahun_awal != '') {
		$thn_awal = $tahun_awal;
	} else {
		$thn_awal = '2000';
	}
	
	if ($tahun_akhir != '') {
		$thn_akhir = $tahun_akhir;
	} else {
		$thn_akhir = date('Y');
	}
	
	$retval = '<select name="'.$name.'">';
	for($i = $thn_akhir; $i > $thn_awal; $i--) {
		$retval .= '<option value="'.$i.'"';
		if ($i == $selected) {
			$retval .= 'selected';
		}
		$retval .= ' >'.$i.'</option>';
	}
	$retval .= '</select>';
	
	return $retval;
}

